/**
 * Created by djh on 2016/11/8.
 * 注册、登录公用js
 *
 */
define(function(require,exports){
   var common = require("common");
   var validate = require("validate");
    /*-------------------end cookie 公共操作方法----------------------------------------*/
    //获取和失去焦点状态
    $(".form-control").focus(function(){
        $(this).parent(".row").addClass("focus");
    }).blur(function(){
        $(this).parent(".row").removeClass("focus");
    });

    /**
     * 登录
     */
    exports.login = function() {
        if(!!exports.wrong){
          common.error(exports.wrong)
        }

        //是否显示验证码
        if(exports.vcodeEnable=="on") {
            $(".vcodeWrap").removeClass("none").find("input").prop("disabled",false);
        }else {
            $(".vcodeWrap").addClass("none").find("input").prop("disabled",true);
        }
        //兼容placeholder
        common.ieTo_placeholder(document.getElementsByTagName('input'), true);
        var $sub_btn = $(".sub_btn");
        var $form = $(".form");
        var $mobile = $("#mobile");
        var $remember = $("#remember");

        $sub_btn.on("click",function(){
            $form.submit();
        });
        //记住密码
        if(Cookie.get("username")!=null) {
            $mobile.val(Cookie.get("username"));
            $remember.prop("checked",true);
        }
        //回车提交表单
        /*common.inputEnterCallback($form.find("input"),function(){
            $form.submit();
        });*/
        $(document).on('keydown',function(e){
            if( e.keyCode==13 ) {
                 $form.submit();
            }
        });
        $form.validate({
            ignore: ".ignore",
            rules:{
                username:{required: true,isMobile:true},
                pwd:{required: true,pwd:[true]},
                vcode:{required: true},
                agree:{required: true}
            },
            messages:{
                username:{required: "请输入用户名"},
                pwd:{required: "请输入密码"},
                vcode:{required: "请输入验证码"},
                agree:{required: "您还没同意隐私协议与服务声明"}
            },

            errorContainer:".errorWrap",
            errorLabelContainer: '.errorWrap',
            wrapper: 'li',
            success: function (label, element) {
                $(element).parent().removeClass("error");

            },
            highlight: function (element, errorClass) {
                $(element).parent().addClass(errorClass);

            },
           submitHandler:function(f){
               layer.load();
               //记住我
               if($remember.prop("checked")&&Cookie.get("username")==null) {
                  Cookie.set("username",$mobile.val(),{path:"/"});
               }else {
                   Cookie.remove("username");
               }
			   f.submit();

           }
        });
    };


    /**
     * 忘记密码
     */
    exports.forget = function() {
        var $step1 = $(".step1");
        var $step2 = $(".step2");
        var $getCodeBtn = $('[data-act="getSMS"]');
        var $mobile = $("#mobile");


        //发送短信验证码
        common.getCode($getCodeBtn,2,$mobile,"/member/reg/getSMS");
        //第一步 找回密码
        $step1.validate({
            ignore: ".ignore",
            rules:{
                username:{required: true,isMobile:true},
                vcode:{required: true}
            },
            messages:{
                username:{required: "请输入手机号码"},
                vcode:{required: "请输入验证码"}
            },
            errorContainer:".step1 .errorWrap",
            errorLabelContainer: '.step1 .errorWrap',
            wrapper: 'li',
            success: function (label, element) {
                $(element).parent().removeClass("error");
                var elementName = element.getAttribute('name'),
                    inValid = element.getAttribute('aria-invalid');
                if(elementName == 'username' && inValid == 'false'){
                    $getCodeBtn.data("active",true).addClass('active').prop("disabled",false);
                }
            },
            highlight: function (element, errorClass) {
                $(element).parent().addClass(errorClass);
                var elementName = element.getAttribute('name'),
                    inValid = element.getAttribute('aria-invalid');
                if(elementName == 'username' && inValid == 'true'){
                    $getCodeBtn.data("active",true).removeClass('active').prop("disabled",true);
                }
            },
            submitHandler:function(){
                common.ajaxHandle({
                    url:exports.step,
                    data:$step1.serialize(),
                    callback:function(item){
                        layer.msg("找回密码成功，请设置新的密码",{end:function(){
                            $step1.remove();
                            $step2.removeClass("none").find("input").prop("disabled",false);
                            $("#token").val(item.data.token);
                        }});
                    }
                })

            }

        });
        //第二步 重新设置密码
        $step2.validate({
            ignore: ".ignore",
            rules:{
                pwd:{required: true,pwd:[true]},
                pwd2:{required: true,equalTo:"#pwd"}
            },
            messages:{
                pwd:{required: "请输入密码"},
                pwd2:{required: "请再次确认密码"}
            },
            errorContainer:".step2 .errorWrap",
            errorLabelContainer: '.step2 .errorWrap',
            wrapper: 'li',
            submitHandler:function(){
                common.ajaxHandle({
                    url:exports.step,
                    data:$step2.serialize(),
                    callback:function(){
                        layer.alert("密码设置成功，请用新的密码登录",{end:function(){
                            location.href="/member/login/index";
                        }});
                    },errorback:function(item) {
                        layer.alert(item.message,{end:function(){
                            location.reload();
                        }});
                    }
                })

            }

        });
        $step2.addClass("none").find("input").prop("disabled",true);
    };

});
